/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jam.sfcc.common.data;

import jam.sfcc.common.domain.Administrador;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author ARIEL
 */
public class AdministradorData extends BaseData {

    public AdministradorData(String userName, String password) {
        super(userName, password);
    }

    public Administrador getAdministrador(int id) throws SQLException {
        Connection conexion = this.getConnection();
        String sqlSelect =
                "SELECT cedula,nombre,apellidos,clave "
                + "FROM Administrador WHERE id_administrador=" + id;
        PreparedStatement statement = conexion.prepareStatement(sqlSelect);
        ResultSet resultSet = statement.executeQuery();
        Administrador administrador = new Administrador();
        if (resultSet.next()) {
            administrador.setCedula(resultSet.getString("cedula"));
            administrador.setNombre(resultSet.getString("nombre"));
            administrador.setApellidos(resultSet.getString("apellidos"));
            administrador.setClave(resultSet.getString("clave"));
            administrador.setIdAdministrador(id);
        }
        statement.close();
        conexion.close();
        return administrador;
    }
    public Administrador getAdministrador2(String nombre,String clave) throws SQLException {
        Connection conexion = this.getConnection();
        String sqlSelect =
                "SELECT id_administrador,cedula,nombre,apellidos,clave "
                + "FROM Administrador WHERE nombre=? AND clave=?";
        PreparedStatement statement = conexion.prepareStatement(sqlSelect);
        statement.setString(1, nombre);
        statement.setString(2, clave);
        ResultSet resultSet = statement.executeQuery();
        Administrador administrador = new Administrador();
        if (resultSet.next()) {
            administrador.setIdAdministrador(resultSet.getInt("id_administrador"));
            administrador.setCedula(resultSet.getString("cedula"));
            administrador.setNombre(resultSet.getString("nombre"));
            administrador.setApellidos(resultSet.getString("apellidos"));
            administrador.setClave(resultSet.getString("clave"));
            
        }
        statement.close();
        conexion.close();
        return administrador;
    }
    public void insertarAdministrador(Administrador administrador) throws SQLException {
        Connection conexion = this.getConnection();
        String sqlInsert =
                "insert into Administrador (cedula,nombre,apellidos,clave) "
                + "values(?,?,?,?)";
        PreparedStatement statement = conexion.prepareStatement(sqlInsert);
        statement.setString(1, administrador.getCedula());
        statement.setString(2, administrador.getNombre());
        statement.setString(3, administrador.getApellidos());
        statement.setString(4, administrador.getClave());
        statement.executeUpdate();
        statement.close();
        conexion.close();
    }

    public void borrarAdministrador(int idAdministrador) throws SQLException {
        Connection conexion = this.getConnection();
        String sqlDelete = "DELETE FROM Administrador WHERE id_administrador=" + idAdministrador;
        PreparedStatement statement = conexion.prepareStatement(sqlDelete);
        statement.executeUpdate();
        statement.close();
        conexion.close();
    }

    public void actualizarAdministrador(Administrador administrador) throws SQLException {
        Connection conexion = this.getConnection();
        String sqlUpdate =
                "UPDATE Administrador"
                + " SET cedula=?, "
                + " nombre=?, "
                + " apellidos=?,"
                + " clave=? WHERE id_administrador=" + administrador.getIdAdministrador();

        PreparedStatement statement = conexion.prepareStatement(sqlUpdate);
        statement.setString(1, administrador.getCedula());
        statement.setString(2, administrador.getNombre());
        statement.setString(3, administrador.getApellidos());
        statement.setString(4, administrador.getClave());
        statement.executeUpdate();
        statement.close();
        conexion.close();
    }

    public LinkedList<Administrador> getAdministradores() throws SQLException {
        Connection conexion = this.getConnection();
        String sqlSelect =
                "SELECT id_administrador,cedula,nombre,apellidos,clave "
                + "FROM Administrador";
        PreparedStatement statement = conexion.prepareStatement(sqlSelect);
        ResultSet resultSet = statement.executeQuery();
        LinkedList<Administrador> administradores = new LinkedList<Administrador>();
        
        while (resultSet.next()) {
            Administrador administrador = new Administrador();
            administrador.setIdAdministrador(resultSet.getInt("id_administrador"));
            administrador.setCedula(resultSet.getString("cedula"));
            administrador.setNombre(resultSet.getString("nombre"));
            administrador.setApellidos(resultSet.getString("apellidos"));
            administrador.setClave(resultSet.getString("clave"));
            administradores.add(administrador);
        }
        statement.close();
        conexion.close();
        return administradores;
    }
}
